package com.easy.project.service.common.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.project.base.exception.ServiceException;
import com.easy.project.constant.FileServiceTypeEnum;
import com.easy.project.convert.FileDomainConvert;
import com.easy.project.domain.dto.common.SysFileDTO;
import com.easy.project.domain.po.system.SysFilePO;
import com.easy.project.domain.vo.common.SysFileVO;
import com.easy.project.mapper.system.SysFileMapper;
import com.easy.project.service.common.ISysFileService;
import com.easy.project.util.MinioUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.function.Function;

import static com.easy.project.base.result.SystemErrorCodeEnum.FILE_UPLOAD_ERROR;

/**
 * minio 文件的实现类
 */
@Slf4j
@Service
@ConditionalOnProperty(name = "file.type",havingValue = "MINIO")
public class MinioFileServiceImpl extends ServiceImpl<SysFileMapper, SysFilePO> implements ISysFileService {

    @Resource
    private FileDomainConvert fileDomainConvert;

    @Resource
    private MinioUtils minioUtils;

    @PostConstruct
    public void init(){
        log.info("文件MINIO服务加载成功");
    }


    @Override
    public String getType() {
        return FileServiceTypeEnum.MINIO.getCode();
    }

    @Override
    public SysFileVO upload(MultipartFile file) {
        String fileName = minioUtils.putObject(file);
        String objectUrl = minioUtils.getObjectUrl(fileName);
        if (Objects.isNull(fileName) || Objects.isNull(objectUrl)) {
            throw new ServiceException(FILE_UPLOAD_ERROR);
        }
        // 保存文件信息
        SysFilePO sysFilePO = new SysFilePO();
        sysFilePO.setFileKey(UUID.randomUUID().toString());
        sysFilePO.setFileType(FileServiceTypeEnum.MINIO.getCode());
        sysFilePO.setFileStoreName(fileName);
        sysFilePO.setFileRealName(file.getOriginalFilename());
        boolean save = save(sysFilePO);
        if (!save){
            log.error("保存文件到数据库失败参数：{}", JSONUtil.toJsonStr(sysFilePO));
            throw new ServiceException(FILE_UPLOAD_ERROR);
        }
        // 组装结果集
        SysFileVO sysFileVO = fileDomainConvert.poToVo(sysFilePO);
        sysFileVO.setPreviewUrl(getPreviewUrl(sysFilePO.getFileKey()));
        return sysFileVO;
    }

    @Override
    public List<SysFileVO> getUrlList(SysFileDTO sysFileDTO) {
        /*List<String> fileKeyList = sysFileDTO.getFileKeyList();
        List<SysFilePO> sysFilePOList = sysFileService.list(new LambdaQueryWrapper<SysFilePO>().in(SysFilePO::getFileKey, fileKeyList));
        for (String fileName : fileNameList) {
            String objectUrl = minioUtils.getObjectUrl(fileName);
            SysFileVO sysFileVO = new SysFileVO();
            sysFileVO.setUrl(objectUrl);
            sysFileVO.setFileName(fileName);
            res.add(sysFileVO);
        }
        return res;*/
        return null;
    }

    @Override
    public void download(String fileKey, HttpServletResponse response) {

    }

    private String getPreviewUrl(String fileKey){
        return minioUtils.getObjectUrl(fileKey);
    }


}
